import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.text.SimpleDateFormat;

public class Game {
	
	private static JFrame startWindow;
	
	public static void main(String[] args) {
		
		final JFrame StartWindow = new JFrame("Adventure");
		StartWindow.setSize(480, 340);
		StartWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		final JPanel panel = new JPanel();
		final JPanel panel2 = new JPanel();
		final JTextField inputarea = new JTextField(15);
		final JTextArea outputarea = new JTextArea(15, 30);
		
		outputarea.append("Welcome to My Adventure Game!\n\nType 'New' to start a new game, or 'load' followed by the name" +
				" of the save file you wish to load. Below is a list of available save files:\n\n" );
		
		File dir = new File("C:\\saves\\");
		File[] children = dir.listFiles();
		
		if (children.length == 0) {
			outputarea.append("You have no saved games at this time, please start a new game.");
		} else {
			outputarea.append("Save Name\tDate\tTime\n\n");
			for (int i = 0; i < children.length; i++) {
				outputarea.append(children[i].toString().substring(9) + "\t");
				long datetime = children[i].lastModified();
				Date d = new Date(datetime);
		        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy        hh:mm:ss");
		        String dateString = sdf.format(d);
		        outputarea.append(dateString + "\n");

			}
		}

		outputarea.setEditable(false);
		outputarea.setLineWrap(true);
		outputarea.setWrapStyleWord(true);
		
		final JButton button = new JButton("Submit");

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
	          {
	              startChoice(inputarea.getText());	
				  inputarea.setText("");
	          }
	      });
		
		
		
		panel.add(inputarea);
		panel.add(button);
		panel2.add(outputarea);
		StartWindow.add(panel, BorderLayout.SOUTH);
		StartWindow.add(panel2, BorderLayout.CENTER);

		StartWindow.setVisible(true);
		startWindow = StartWindow;
		
		
		
	}
		public static void startChoice(String a) {
		
		ArrayList<String> response = new ArrayList<String>();
		String Command = null;
		String Target = null;
			
		StringTokenizer Wordbreaker = new StringTokenizer(a);
		
		while (Wordbreaker.hasMoreTokens()) {
			response.add(Wordbreaker.nextToken());
		}
		
		Command = response.get(0);
			
		
		if (Command.equalsIgnoreCase("New")) {
		
			GameState game = new GameState();
			final CommandManager CmdMan = new CommandManager();
			final Player player = new Player();
			GUI Gui = new GUI(CmdMan, player);
			CmdMan.setBattleMan(Gui);
			World world = new World(game, Gui, player);
			world.GenerateWorld();
			CmdMan.setGameState(game);
		
			startWindow.setVisible(false);

			Gui.init();
		

		} else if (response.get(0).equalsIgnoreCase("load")){
			
			
			String nextChoice = response.get(1);
			
			
			if (nextChoice.substring(nextChoice.length() - 4, nextChoice.length()).equalsIgnoreCase(".sav")) {

			} else {
				nextChoice = (nextChoice + ".sav");
			}

			
			GameState game = null;
			FileInputStream fis = null;
			ObjectInputStream in = null;
			try
			{
			fis = new FileInputStream("C:\\saves\\" + nextChoice);
			in = new ObjectInputStream(fis);
			game = (GameState)in.readObject();
			in.close();
			}
			catch(IOException ex)
			{
			ex.printStackTrace();
			}
			catch(ClassNotFoundException ex)
			{
			ex.printStackTrace();
			}
			startWindow.setVisible(false);
			game.getGUI().init(); 
		}
	
		
	
		}
	

}
